#!/bin/bash
# HTML-Previewer


#variable declariations
PREVIEW="UGPreview";


#start the script
echo "Creating an HTML preview may take up to 2 minutes.";
echo;

echo -n "Preparing.(1 of 5).................................";
cd ../ #moving out of working copy
mkdir $PREVIEW; #create exporting dir
cd $PREVIEW; #move to exporting dir
mkdir img; #create a directory to store icons
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"" >> Preview.htm; #html header part 1
echo "\"http://www.w3.org/TR/html4/strict.dtd\">" >> Preview.htm; #html header part 2
echo "<html>" >> Preview.htm;
echo "\t<head>" >> Preview.htm;
echo "\t\t<style type=\"text/css\">" >> Preview.htm; #adding CSS to the file
echo "\t\t\timg {border:0px;}" >> Preview.htm; #CSS for images
echo "\t\t</style>" >> Preview.htm; #end of CSS
echo "\t</head>" >> Preview.htm;
echo "\t<body>" >> Preview.htm;
echo "\t\t<div>" >> Preview.htm;
cd img;
echo "[DONE]";

echo -n "Creating subdirectories.(2 of 5)...................";
for folder in $FOLDERS
do
	mkdir $folder;
done
cd ..; #moving to $PREVIEW
echo "[DONE]";


echo -n "Exporting icons.(3 of 5)...........................";        
cd ../$PROJECT/$SOURCES
SVG=`find ./ -name *.svg -type f`                           
for file in $SVG
do
	inkscape --without-gui --export-area-canvas --export-width=48 --export-height=48 --file=$file --export-png=../../$PREVIEW/img/$file >InkscapeExportOutput.tmp
done 
rm -f InkscapeExportOutput.tmp
echo "[DONE]";

echo -n "Renaming icons and filling the page.(4 of 5).......";
cd ../../$PREVIEW      
#use the same SVG list from the previous block                          
for file in $SVG
do
	BEGIN="/"; #file ./name.svg
	POS=`expr index "$file" $BEGIN`; #position of /name.svg
	POS=`expr $POS + 1`; #position of name.svg
	LENGTH=${#file}; #this is the length of ./name.svg
	file=`expr substr "$file" $POS $LENGTH`; #file now has name.svg
	oldfile=$file; #oldfile has name.svg
	LENGTH=${#file}; #this is the length of name.svg
	LENGTH=`expr $LENGTH - 4`; #this is the length of name
	file=`expr substr "$file" 1 $LENGTH`;
	file=$file".png"; #file has the name that the new symlink should have name.png
	mv img/$oldfile img/$file;
	echo "\t\t\t<img src=\"img/"$file"\" /> " >> Preview.htm; #appending the icon to the file
done
echo "[DONE]";



echo -n "Closing the HTML file.(5 of 5).....................";
echo "\t\t</div>" >> Preview.htm;
echo "\t</body>" >> Preview.htm;
echo "</html>" >> Preview.htm;
echo  "[DONE]"; 
echo "Open '"$HOME/$PREVIEW/"Preview.htm' to take a screen shot.";

echo;

exit 0
